from app.core.code_msg import CodeMsg, SUCCESS


class ApiException(Exception):
    def __init__(self, *, code_msg: CodeMsg = None, code: int = SUCCESS.code, message: str = SUCCESS.message,
                 data: dict = None):
        if code_msg:
            self.code = code_msg.code
            self.message = code_msg.message
        else:
            self.code = code
            self.message = message
        self.data = data

    @staticmethod
    def error(*, code_msg: CodeMsg = None, code: int = 500, message: str = "操作失败", data: dict = None):
        if code_msg:
            return ApiException(code=code_msg.code, message=code_msg.message, data=data)
        return ApiException(code=code, message=message, data=data)

    @staticmethod
    def success(*, code: int = 0, message: str = "操作成功", data: dict = None):
        return ApiException(code=code, message=message, data=data)
